/*-------------------------------------------------------------------------
File Name: Scpi.c
Function Description: the scpi command driver define.
Author: ray.gong (2012-11-10)
Modify history:
-------------------------------------------------------------------------*/
#include <types.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <bus/sci/sci.h>
#include <bus/dog/dog.h>
#include <hld/hld_dev.h>
#include "scpi.h"
#include "scpi_cmd_drv.h"


/*-------------------------------------------------------------------------
Function Name: drvStr2Hex
Function Description: format string like "0x123456789" to hex  .
Author: ray.gong (2012-11-10)
Input parameter: szInput:input string;
				 puiVal:point to the value after format 
				 
Output parameter:
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/
UINT32 drvStr2Hex(CHAR *szInput,UINT32 *puiVal)
{
	UINT32 i=0;
	UINT32 uiRtn=-1;
	UINT32 uiValTmp=0;
	UINT32 uiDataType=0;
	UINT32 uiPowVal=0;
	
	if((NULL==szInput)||(strlen(szInput)<3))
	{
		return -1;
	}

	if(((szInput[0]!='0'))||((szInput[1]!='x')&&(szInput[1]!='X')))
	{
		return -1;
	}

	for(i=0;i<strlen(szInput)-2;i++)
	{
		if((szInput[2+i]>='0')&&(szInput[2+i]<='9'))
		{
			//uiPowVal=drvPower(16,(strlen(szInput)-3-i));
			uiPowVal=1<<(4*(strlen(szInput)-3-i));
			if(-1==uiPowVal)
			{
				return -1;
			}				
			uiValTmp+=(szInput[2+i]-'0')*uiPowVal;  
		}
		else if((szInput[2+i]>='a')&&(szInput[2+i]<='f'))
		{
			//uiPowVal=drvPower(16,(strlen(szInput)-3-i));
			uiPowVal=1<<(4*(strlen(szInput)-3-i));
			if(-1==uiPowVal)
			{
				return -1;
			}				
			uiValTmp+=(szInput[2+i]-'a'+10)*uiPowVal;  		}
		else if((szInput[2+i]>='A')&&(szInput[2+i]<='F'))
		{
			//uiPowVal=drvPower(16,(strlen(szInput)-3-i));
			uiPowVal=1<<(4*(strlen(szInput)-3-i));
			if(-1==uiPowVal)
			{
				return -1;
			}				
			uiValTmp+=(szInput[2+i]-'A'+10)*uiPowVal;  		
		}
		else
		{
			return -1;
		}
	}

	*puiVal=uiValTmp;
	return 0;
}


/*-------------------------------------------------------------------------
Function Name: drvReadMem
Function Description: read memory   .
Author: ray.gong (2012-11-10)
Input parameter: uiMode:0:8bit once read;1:16bit once read;2:32bit once read;
				 uiAddr:memory address 
				 
Output parameter:
				puiVal:point to the read value of uiAddr
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/
UINT32 drvReadMem(UINT32 uiMode,UINT32 uiAddr,UINT32 *puiVal)
{
	UINT32 uiTmp=0;
	UINT32 uiRtn=-1;
	
	if((NULL==uiAddr)||(NULL==puiVal)||(uiMode>BIT_32))
	{
		return -1;
	}

	switch(uiMode)
	{
		case BIT_8:
			uiTmp=*(UINT8 *)(uiAddr);
			uiRtn=0;
			break;
		case BIT_16:
			uiTmp=*(UINT16 *)(uiAddr);
			uiRtn=0;
			break;
		case BIT_32:
			uiTmp=*(UINT32 *)(uiAddr);
			uiRtn=0;
			break;
		default:
			uiRtn=-1;
			break;
	}

	if(0==uiRtn)
	{
		*puiVal=uiTmp;
	}
	return uiRtn;
}

/*-------------------------------------------------------------------------
Function Name: drvWriteMem
Function Description: read MEMORY   .
Author: ray.gong (2012-11-10)
Input parameter: uiMode:0:8bit once read;1:16bit once read;2:32bit once read;
				 uiAddr:registor address 
				 uiVal:the value want to be write to address uiAddr
				 
Output parameter:NONE
				
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/
UINT32 drvWriteMem(UINT32 uiMode,UINT32 uiAddr,UINT32 uiVal)
{
	UINT32 uiTmp=0;
	UINT32 uiRtn=-1;
	
	if((NULL==uiAddr)||(uiMode>BIT_32))
	{
		return -1;
	}

	switch(uiMode)
	{
		case BIT_8:
			*(UINT8 *)(uiAddr)=(UINT8)(uiVal&0xff);
			uiRtn=0;
			break;
		case BIT_16:
			*(UINT16 *)(uiAddr)=(UINT16)(uiVal&0xffff);
			uiRtn=0;
			break;
		case BIT_32:
			*(UINT32 *)(uiAddr)=(UINT32)(uiVal);
			uiRtn=0;
			break;
		default:
			uiRtn=-1;
			break;
	}

	return uiRtn;
}

/*-------------------------------------------------------------------------
Function Name: drvReadReg
Function Description: read registor   .
Author: ray.gong (2012-11-10)
Input parameter: uiMode:0:8bit once read;1:16bit once read;2:32bit once read;
				 uiAddr:registor address 
				 
Output parameter:
				puiVal:point to the read value of uiAddr
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/
UINT32 drvReadReg(UINT32 uiMode,UINT32 uiAddr,UINT32 *puiVal)
{
	UINT32 uiTmp=0;
	UINT32 uiRtn=-1;
	
	if((NULL==uiAddr)||(NULL==puiVal)||(uiMode>BIT_32))
	{
		return -1;
	}

	switch(uiMode)
	{
		case BIT_8:
			uiTmp=*(volatile UINT8 *)(uiAddr);
			uiRtn=0;
			break;
		case BIT_16:
			uiTmp=*(volatile UINT16 *)(uiAddr);
			uiRtn=0;
			break;
		case BIT_32:
			uiTmp=*(volatile UINT32 *)(uiAddr);
			uiRtn=0;
			break;
		default:
			uiRtn=-1;
			break;
	}

	if(0==uiRtn)
	{
		*puiVal=uiTmp;
	}
	return uiRtn;
}

/*-------------------------------------------------------------------------
Function Name: drvWriteReg
Function Description: read registor   .
Author: ray.gong (2012-11-10)
Input parameter: uiMode:0:8bit once read;1:16bit once read;2:32bit once read;
				 uiAddr:registor address 
				 uiVal:the value want to be write to address uiAddr
				 
Output parameter:NONE
				
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/

UINT32 drvWriteReg(UINT32 uiMode,UINT32 uiAddr,UINT32 uiVal)
{
	UINT32 uiTmp=0;
	UINT32 uiRtn=-1;
	
	if((NULL==uiAddr)||(uiMode>BIT_32))
	{
		return -1;
	}

	switch(uiMode)
	{
		case BIT_8:
			*(volatile UINT8 *)(uiAddr)=(UINT8)(uiVal&0xff);
			uiRtn=0;
			break;
		case BIT_16:
			*(volatile UINT16 *)(uiAddr)=(UINT16)(uiVal&0xffff);
			uiRtn=0;
			break;
		case BIT_32:
			*(volatile UINT32 *)(uiAddr)=(UINT32)(uiVal);
			uiRtn=0;
			break;
		default:
			uiRtn=-1;
			break;
	}

	return uiRtn;
}

UINT32 drvGetDumpMem(ModuleID MdlId,UINT32 uiMemLen,UINT32 *puiMemAddr)
{
	UINT32 uiRtn=-1;
#if 0
	switch(MdlId)
	{
		case MDL_DECA:
			*puiMemAddr=drvDecaDumpInit(uiMemLen);
			break;
		case MDL_SND:
			*puiMemAddr=drvSndDumpInit(uiMemLen);
			break;
		default:
			return -1;
	}

	if((-1!=*puiMemAddr)&&(0!=*puiMemAddr))
	{
		uiRtn=0;
	}
	else
	{
		uiRtn=-1;
	}
#endif
	return uiRtn;
}

/*-------------------------------------------------------------------------
Function Name: drvReleaseDumpMem
Function Description: release the memory for module dump after end dump operate   .
Author: ray.gong (2012-11-10)
Input parameter: MdlId:see data type ModuleID
				 
Output parameter:NONE
Return Value:SUCCESS:RET_SUCCESS: return success;RET_FAILURE: retrun failed
Modify history:
-------------------------------------------------------------------------*/
UINT32 drvReleaseDumpMem(ModuleID MdlId)
{
#if 0
	switch(MdlId)
	{
		case MDL_DECA:
			drvDecaDumpDebugRelease();
			break;
		case MDL_SND:
			drvSndDumpDebugRelease();
			break;
		default:
			return -1;
	}
#endif
	return 0;
}
